package org.example.trigger.http;


import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.example.api.IOrderApi;
import org.example.api.dto.LockOrderReqDTO;
import org.example.api.dto.LockOrderResDTO;
import org.example.api.response.Response;
import org.example.domain.order.model.entity.OrderEntity;
import org.example.domain.order.service.IOrderService;
import org.example.types.enums.ResponseCode;
import org.example.types.utils.ThreadLocalUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author yinghuashen
 * @version 1.0
 * @description
 * @date 2025/3/10 20:36
 */

@Slf4j
@RestController
@CrossOrigin("*")
@RequestMapping("/order")
public class OrderController implements IOrderApi {

    @Resource
    private IOrderService orderService;

    @Override
    @PostMapping("/lock_order")
    public Response<LockOrderResDTO> lockOrder(LockOrderReqDTO lockOrderReqDTO) {

        String userId = ThreadLocalUtils.getCurrentUserId();
        String productId = lockOrderReqDTO.getProductId();
        int count = lockOrderReqDTO.getCount();

//        String orderId = orderService.lockOrder(userId, productId, count);
        String orderId = orderService.lockOrder_v2(OrderEntity.builder().userId(userId).productId(productId).productAmount(count).build());
        if(StrUtil.isBlank(orderId)){
            return Response.<LockOrderResDTO>builder().
                    code(ResponseCode.ORDER_LOCK_ERROR.getCode()).
                    info(ResponseCode.ORDER_LOCK_ERROR.getInfo()).
                    data(null).build();
        }
        LockOrderResDTO lockOrderResDTO = LockOrderResDTO.builder().orderId(orderId).build();
        return Response.<LockOrderResDTO>builder().
                code(ResponseCode.SUCCESS.getCode()).
                info(ResponseCode.SUCCESS.getInfo()).
                data(lockOrderResDTO).build();
    }
}
